//
//  PPConversationTableViewCell.m
//  pet
//
//  Created by 陈无忌 on 11/11/14.
//  Copyright (c) 2014 whalefin. All rights reserved.
//

#import "PPConversationCell.h"

@implementation PPConversationCell

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)configCellWithConversation: (PPConversation *)conversation
{
    //chatter name
    if (conversation.isGroup == 0) {
        //非群组聊天
        self.nameLabel.text = conversation.chatUser.userName;
        [self.avatar setImageWithURL:[PPImageUrl getImageUrl:conversation.chatUser.userAvatar]];
    }else{
        //群组聊天
        self.nameLabel.text = conversation.chatGroup.groupName;
        [self.avatar setImage:[UIImage imageNamed:@"groupAvatar"]];
    }
    //content
    EMMessage *msg = [conversation.conversation latestMessage];
    if (msg) {
        id messageBody = msg.messageBodies.lastObject;
        if ([messageBody isKindOfClass:[EMTextMessageBody class]]) {
            EMTextMessageBody *message = (EMTextMessageBody *)messageBody;
            self.lastMessageLabel.text = message.text;
        }else if ([messageBody isKindOfClass:[EMImageMessageBody class]]){
            self.lastMessageLabel.text = @"[图片]";
        }else if ([messageBody isKindOfClass:[EMVideoMessageBody class]]){
            self.lastMessageLabel.text = @"[视频]";
        }else if ([messageBody isKindOfClass:[EMVoiceMessageBody class]]){
            self.lastMessageLabel.text = @"[声音]";
        }else if ([messageBody isKindOfClass:[EMLocationMessageBody class]]){
            self.lastMessageLabel.text = @"[位置]";
        }else{
            self.lastMessageLabel.text = @"";
        }
    }else{
        switch (conversation.isGroup) {
            case 0:
                self.lastMessageLabel.text = @"你们已经是好友了，快开始聊天吧";
                break;
            case 1:
                self.lastMessageLabel.text = [NSString stringWithFormat:@"你们加入了群%@，快开始聊天吧",self.nameLabel.text];
                break;
                
            default:
                break;
        }
    }
    //时间转换
    NSDate *date = [NSDate date];
    NSTimeInterval secondsSince2001 = [date timeIntervalSinceReferenceDate];
    NSTimeInterval secondsSince1970 = [date timeIntervalSince1970];
    long long difference = trunc(secondsSince2001 - secondsSince1970);
    long long intDate = msg.timestamp/1000 + difference;
    date = [NSDate dateWithTimeIntervalSinceReferenceDate:intDate];
    self.lastTimeLabel.text = [self setTimeWithDate:date];
    //角标
    if (conversation.conversation.unreadMessagesCount > 0) {
        self.subscriptButton.hidden = NO;
        [self.subscriptButton setTitle:[NSString stringWithFormat:@"%d",conversation.conversation.unreadMessagesCount] forState:UIControlStateNormal];
    }else{
        self.subscriptButton.hidden = YES;
    }
}

- (NSString *)setTimeWithDate:(NSDate *)date
{
    //
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    NSString *dateStr = [formatter stringFromDate:date];
    NSString *nowdateStr = [formatter stringFromDate:[NSDate date]];
    NSString *yesterdayStr = [formatter stringFromDate:[NSDate dateWithTimeInterval:24*60*60 sinceDate:[NSDate date]]];
    
    if ([dateStr isEqualToString:nowdateStr]) {
        //是否是今天
        [formatter setDateFormat:@"HH:mm"];
        return [NSString stringWithFormat:@"今天 %@",[formatter stringFromDate:date]];
    }else if ([dateStr isEqualToString:yesterdayStr]){
        //是否是昨天
        return @"昨天";
    }else if ([dateStr isEqualToString:@"1970-01-01"]) {
        return @"";
    }else{
        //其他时间
        [formatter setDateFormat:@"yyyy/MM/dd"];
        return [formatter stringFromDate:date];
    }
    
    
}

@end
